﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] != null)
        {
            int id = Convert.ToInt32(Request.QueryString["id"].ToString());

            if (id == 1 || id == 2)
            {
                if (Session["WelcomeUser"] != null)
                {
                    Response.Redirect("~/LoginMessage.aspx?id=0");
                }

                if (id == 1)
                {
                    Label1.Text = "Student Login";
                }
                else if (id == 2)
                {
                    Label1.Text = "Employee Login";
                }
            }
            else
            {
                Response.Redirect("~/ErrorPage.aspx");
            }
        }
        else
        {
            Response.Redirect("~/ErrorPage.aspx");
        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

        if (udm.createConnection() != null)
        {
            int id = Convert.ToInt32(Request.QueryString["id"].ToString());

            GenerateDates gd = new GenerateDates();

            string username = txtUsername.Text;
            string password = gd.EncodePassword(txtPassword.Text);

            if (id == 1)
            {
                if (udm.login(udm.createConnection(), 1, username, password) != null)
                {
                    DataSet result = udm.login(udm.createConnection(), 1, username, password);

                    if (result.Tables[0].Rows.Count > 0)
                    {
                        Session["StudentID"] = result.Tables[0].Rows[0][0].ToString();
                        Session["WelcomeUser"] = username;

                        Response.Redirect("~/Default.aspx");
                    }
                }
                else
                {
                    Response.Redirect("~/ErrorPage.aspx");
                }
            }
            else if (id == 2)
            {
                if (udm.login(udm.createConnection(), 2, username, password) != null)
                {
                    DataSet result = udm.login(udm.createConnection(), 2, username, password);

                    if (result.Tables[0].Rows.Count > 0)
                    {
                        Session["EmployeeID"] = result.Tables[0].Rows[0][0].ToString();
                        Session["Role"] = result.Tables[0].Rows[0][1].ToString();
                        Session["WelcomeUser"] = username;

                        Response.Redirect("~/Default.aspx");
                    }
                }
                else
                {
                    Response.Redirect("~/ErrorPage.aspx");
                } 
            }

            Session.Timeout = 20;
        }
        else
        {
            Response.Redirect("~/ErrorPage.aspx");
        }
    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

        GenerateDates gd = new GenerateDates();

        string username = txtUsername.Text;
        string password = gd.EncodePassword(txtPassword.Text);

        int id = Convert.ToInt32(Request.QueryString["id"].ToString());

        if (id == 1)
        {
            if (udm.login(udm.createConnection(), 1, username, password) != null)
            {
                DataSet result = udm.login(udm.createConnection(), 1, username, password);

                if (result.Tables[0].Rows.Count > 0)
                {
                    args.IsValid = true;
                }
                else
                {
                    args.IsValid = false;
                }
            }
            else
            {
                Response.Redirect("~/ErrorPage.aspx");
            }
        }
        else if (id == 2)
        {
            if (udm.login(udm.createConnection(), 2, username, password) != null)
            {
                DataSet result = udm.login(udm.createConnection(), 2, username, password);

                if (result.Tables[0].Rows.Count > 0)
                {
                    args.IsValid = true;
                }
                else
                {
                    args.IsValid = false;
                }
            }
            else
            {
                Response.Redirect("~/ErrorPage.aspx");
            }
        }
    }
}